# -*- coding: utf-8 -*-
from .sql import Author,Poem
import requests

class PoemPipeline(object):
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'}
    def process_item(self, item, spider):
        if 'body' in item:
            print('in')
            poem=Poem()
            poem.auth=item['author']
            poem.dynasty=item['dynasty']
            poem.name=item['title']
            poem.body=item['body']
            tags=item['tags']
            tags="*".join(tags)
            poem.tags=tags
            self.session.add(poem)
            print('out')
            try:
                print("commit")
                self.session.commit()
            except:
                print("*"*30,"保存失败")
        else:
            print('in')
            auth=Author()
            auth.name=item['author']
            auth.simple=item['simple']
            auth.img=item['img']
            self.session.add(auth)
            print('out')
            try:
                print("commit")
                self.session.commit()
            except:
                print("#"*30,"保存失败")
        return item

    def __init__(self):
        self.session=None

    def open_spider(self,spider):
        from .sql import session
        self.session=session

